import FlowGraph from '../../../graph/index'

export function changeEdge(type, oldEdge, config) {
  const { graph } = FlowGraph
  const { id, target, source, index, router, attrs, shape } = oldEdge

  if (type === 'double-edge') {
    if (shape !== 'double-edge') {
      graph.removeEdge(id)
      return graph.addEdge({
        shape: 'double-edge',
        id,
        target,
        source,
        index,
        router,
        attrs: {
          line: {
            stroke: '#ffffff',
            targetMarker: {
              stroke: config.stroke
            }
          },
          outline: {
            strokeDasharray: config.strokeDasharray,
            stroke: config.stroke
          }
        }
      })
    } else {
      const {
        line: { strokeWidth, targetMarker }
      } = attrs
      const y = strokeWidth / 2
      const d = `M 10 -${y} 10 -10 -2 0 10 10 10 ${y}`
      if (!targetMarker) {
        oldEdge.attr('line/targetMarker', { stroke: config.stroke, d, tagName: 'path' })
      } else {
        oldEdge.attr('line/targetMarker/d', d)
      }
      return oldEdge
    }
  }
  if (type === 'double-line') {
    if (shape !== 'double-edge') {
      graph.removeEdge(id)
      return graph.addEdge({
        shape: 'double-edge',
        id,
        target,
        source,
        index,
        router,
        attrs: {
          line: {
            stroke: '#ffffff',
            // targetMarker: {
            //   stroke: config.stroke,
            //   d: 'M-3 -2.5 l10 0 M-3 2.5 l10 0'
            // }
            targetMarker: null
          },
          outline: {
            strokeDasharray: config.strokeDasharray,
            stroke: config.stroke
          }
        }
      })
    } else {
      // const strokeWidth = attrs.line.strokeWidth
      // const y = strokeWidth / 2 - 0.5
      // oldEdge.attr('line/targetMarker/d', `M-3 -${y} l10 0 M-3 ${y} l10 0`)
      oldEdge.attr('line/targetMarker', null)
      return oldEdge
    }
  }
  if (['block', 'classic', 'diamond', 'circle', 'circlePlus', 'cross', 'async', 'ellipse', 'line'].includes(type)) {
    let targetMarker = {
      name: type
    }
    if (['circle', 'circlePlus'].includes(type)) {
      targetMarker.r = config.strokeWidth + 4
    } else if (type === 'ellipse') {
      targetMarker.rx = config.strokeWidth + 7
      targetMarker.ry = config.strokeWidth + 4
    } else if (type === 'line') {
      // targetMarker.d = '' // `M-15 -${y} L0 -${y} L0 ${y} L-15 ${y} Z`
      // targetMarker.name = 'path'
      targetMarker = null
    } else {
      targetMarker.size = config.strokeWidth + 7
    }
    if (shape !== 'edge') {
      graph.removeEdge(id)
      return graph.addEdge({
        shape: 'edge',
        id,
        target,
        source,
        index,
        router,
        attrs: {
          line: {
            strokeWidth: config.strokeWidth,
            stroke: config.stroke,
            strokeDasharray: config.strokeDasharray,
            targetMarker
          }
        }
      })
    } else {
      oldEdge.attr('line/targetMarker', targetMarker)
      return oldEdge
    }
  }
  return oldEdge
}

export function changeStrokeWidthAndArrow(Edge, width, type) {
  const {
    shape,
    attrs: {
      line: { targetMarker }
    }
  } = Edge
  if (shape === 'edge') {
    Edge.attr('line/strokeWidth', width)
    if (!targetMarker) {
      return
    }
    if (['circle', 'circlePlus'].includes(targetMarker.name)) {
      targetMarker.r = width + 4
    } else if (targetMarker.name === 'ellipse') {
      targetMarker.rx = width + 7
      targetMarker.ry = width + 4
    } else if (targetMarker.name !== 'path') {
      targetMarker.size = width + 7
    }
    Edge.attr('line/targetMarker', targetMarker)
  }
  if (shape === 'double-edge') {
    Edge.attr('line/strokeWidth', width - 2)
    Edge.attr('outline/strokeWidth', width)
    if (type === 'double-edge') {
      const y = width / 2
      Edge.attr('line/targetMarker/d', `M 10 -${y} 10 -10 -2 0 10 10 10 ${y}`)
    } else {
      const y = width / 2 - 0.5
      Edge.attr('line/targetMarker/d', `M-3 -${y} l10 0 M-3 ${y} l10 0`)
    }
  }
}
